package cn.shenlong.portal.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Application;
import cn.shenlong.portal.mapper.ApplicationMapper;
import cn.shenlong.portal.service.ApplicationService;

@Service("applicationService")
public class ApplicationServiceImpl implements ApplicationService {

	@Autowired
	private ApplicationMapper applicationMapper;

	@Override
	public int saveEntity(Application entity) {
		entity.setDataflg(1l);
		entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		return applicationMapper.saveEntity(entity);
	}

	@Override
	public int updateEntity(Application entity) {
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		return applicationMapper.updateEntity(entity);
	}

	@Override
	public Application findByPK(Serializable entityId) {
		return applicationMapper.findByPK(entityId);
	}

	@Override
	public int deleteByPKArray(Serializable[] ids) {
		if (ids != null && ids.length > 0) {
			applicationMapper.deleteByPKArray(ids);
			return 0;
		}
		return 1;
	}

	@Override
	public Pagination findPageListByCriteria(Integer pageNo, Integer pageSize,
			Map<String, Object> para) {
		RowBounds rowBounds = new RowBounds();
		if (pageNo != null && pageSize != null) {
			rowBounds = new RowBounds((pageNo - 1) * pageSize, pageSize);
		}
		Long count = applicationMapper.findCountByCriteria(para);

		List<Application> list = applicationMapper.findPageListByCriteria(para,
				rowBounds);
		Pagination page = new Pagination(pageNo, pageSize, count.intValue(),
				list);
		return page;
	}

	@Override
	public List<Application> getNaOrLa(Long applicationType, Long priority) {
		if (applicationType != null) {
			if (priority != null) {
				return applicationMapper.getNaOrLaByPriority(applicationType,
						priority);
			} else {
				return applicationMapper.getNaOrLa(applicationType);
			}
		}
		return null;
	}

	@Override
	public List<Application> getNaOrLaByChannelId(Long applicationType,
			Long channelId) {
		if (applicationType != null && channelId != null) {
			return applicationMapper.getNaOrLaByChannelId(applicationType,
					channelId);
		}
		return null;
	}

}
